package blogtest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogTest extends InitAndEnd{
    /*
        测试登录 跳转到    http://localhost:8080/myblog_list.html
     */
    @Test
    void loginTest(){
        //打开博客登录页
            webDriver.get("https://www.baidu.com");
            webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
            //输入用户名和密码
            webDriver.findElement(By.cssSelector("#username")).sendKeys("xiaowen");
            webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
            webDriver.findElement(By.cssSelector("#submit")).click();
            //验证
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url =  webDriver.getCurrentUrl();
      //  Assertions.assertEquals("http://localhost:8080/myblog_list.html",cur_url) ;
    }
    @Test
    void test(){
        webDriver.get("https//www.baidu.com");
    }
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
     void LoginSuccess(String username, String password, String blog_list_url) {
        System.out.println(username + password + blog_list_url);
        // 打开博客登录页面
        webDriver.get("https://www.baidu.com");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入账号admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 跳转到列表页
        // 获取到当前页面url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 如果url=http://42.192.83.143:8563/blog_system/blog_list.html,测试通过，否则测试不通过
        Assertions.assertEquals(blog_list_url, cur_url);
        // 列表页展示用户信息是admin
        // 用户名是admin测试通过，否则测试不通过
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
        Assertions.assertEquals(username, cur_admin);
    }
}
